package sein.android.hopenote;

import java.util.ArrayList;

import sein.android.hopenote.Const.HopeNoteDb;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.Toast;

public class HopeNoteMainActivity extends TabActivity {

	private DBAdapter HnopenHelper;
	private SQLiteDatabase sdb;
	private TabHost tabHost;
	
	//Intent note_search;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.hopenote_main);
        //app 최초실행시 database 생성
        HnopenHelper = new DBAdapter(this);
        
        sdb = HnopenHelper.getReadableDatabase();
        
        //TabHost tabHost = (TabHost)findViewById(R.id.tabhost);
        tabHost = getTabHost();
        //tabHost.setup();
        
        
        setTitle(getString(R.string.user_name));
        
        HopeNoteDb.CURRENT_USER = getString(R.string.user_name);
        
        //LayoutInflater inflater = LayoutInflater.from(this);
        //inflater.inflate(R.layout.hopenote_main, tabHost.getTabContentView(),true);
        
        //TabHost.TabSpec spec = ;        
        //spec.setIndicator("입력");
        tabHost.addTab(tabHost.newTabSpec("tag1").setIndicator("입력").setContent(new Intent(this,Note_Input_Tab.class)));
        
        //spec = tabHost.newTabSpec("tag2");
        //spec.setContent(R.id.tab_search);
        //spec.setIndicator("검색");
        //tabHost.addTab(spec.setContent(new Intent(this,Note_Search.class)));
        tabHost.addTab(tabHost.newTabSpec("tag2").setIndicator("검색").setContent(new Intent(this,Note_Search_Tab.class)));
        tabHost.addTab(tabHost.newTabSpec("tag3").setIndicator("챠트").setContent(new Intent(this,Note_Chart_Tab.class)));
        
        
        
     // Tab Widget 높이 조절
        for (int tab = 0; tab < tabHost.getTabWidget().getChildCount(); ++tab) {
        	tabHost.getTabWidget().getChildAt(tab).getLayoutParams().height *= 0.5;//= HopeNoteDb.TAB_WIDGET_HEIGHT;
        }
        
        tabHost.setCurrentTab(0);
    }
   

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //getMenuInflater().inflate(R.menu.activity_main, menu);
		super.onCreateOptionsMenu(menu);
		MenuItem item = null;
		switch(tabHost.getCurrentTab()){
		case 0:
			//item = menu.add(0,1,0,"짜장");
			//item.setIcon(R.drawable.ic_action_search);
			//item.setAlphabeticShortcut('a');
			menu.add(0,1,0,"사용자").setIcon(R.drawable.ic_action_search);
			menu.add(0,2,0,"항목등록").setIcon(R.drawable.ic_action_search);
			return true; 
		case 1:
			menu.add(0,1,0,"사용자").setIcon(R.drawable.ic_action_search);
			menu.add(0,3,0,"검색").setIcon(R.drawable.ic_action_search);
			return true;
		case 2:
			menu.add(0,1,0,"사용자").setIcon(R.drawable.ic_action_search);
			menu.add(0,3,0,"검색").setIcon(R.drawable.ic_action_search);
			return true;
		}
		return false;		
    }
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		//getTabHost();
		TabHost.TabSpec spec = null;
		switch(item.getItemId()){
		case 1:
			Intent list = new Intent(this, User_List.class);
			startActivity(list);
			return true;
		case 2:
			Intent item_input = new Intent(this, Item_Input.class);
			startActivity(item_input);			
			return true;
		case 3:
			
			/*
			final Intent note_search = new Intent(this, Note_Search.class);
			//get category from DB
			Cursor grp_cur = sdb.query(HopeNoteDb.TB_ITEM, new String[] {HopeNoteDb.COL_GROUP}, null, null, HopeNoteDb.COL_GROUP, null, null);
			grp_cur.moveToFirst();
	        final CharSequence[] grp_list = new CharSequence[grp_cur.getCount()];
	        for(int i = 0; i < grp_cur.getCount(); i++){
	        	grp_list[i] = grp_cur.getString(0);
	        	grp_cur.moveToNext();
	        }
	        grp_cur.close();
	        
	        //set Dialog
			AlertDialog.Builder chooseCtg = new AlertDialog.Builder(HopeNoteMainActivity.this);
			chooseCtg.setTitle("항목 선택");
			chooseCtg.setItems(grp_list, new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					note_search.putExtra("category",grp_list[which].toString());
					View v = Note_Search_Tab.SearchTabGroup.getLocalActivityManager().startActivity("Group_Sel", note_search.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
					Note_Search_Tab.SearchTabGroup.replaceView(v);
				}
			});
			chooseCtg.show();
			*/
			
			Intent item_set = new Intent(this, Item_Set.class);
			startActivityForResult(item_set, HopeNoteDb.REQ_ITEM_SET);
			return true;
			
			
			
		}
		return false;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode){
		case HopeNoteDb.REQ_ITEM_SET:
			if(resultCode == RESULT_OK){
				Intent note_search = new Intent(this, Note_Search.class);
				note_search.putExtra(HopeNoteDb.EXTRA_SEARCH,data.getStringExtra(HopeNoteDb.EXTRA_MAIN));
				View v = Note_Search_Tab.SearchTabGroup.getLocalActivityManager().startActivity("Note_Search", note_search.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
				Note_Search_Tab.SearchTabGroup.replaceView(v);
			}
			break;
		}
	}
    
    
}
